/*
Copyright (c) 2008,2009, David Beck

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**
   @file t__t1_client.cc
   @brief verifies the client side of various security models

   The actual security model used depends on the build. The following
   implementations are used:

   @li t__sec_null.cc the rxnull_* model
   @li t__sec_simple.cc hand crafted security model to see how Rx works
   @li t__sec_rt0d4.cc the rt0d4/Rx security model

   @file t__t1.cs.c
   @brief Automatically generated by rxgen

   @file t__t1.h
   @brief Automatically generated by rxgen

   @file t__t1.xdr.c
   @brief Automatically generated by rxgen
*/

extern "C" {
  #include "t__t1.h"
}

#define RT0D4_WINDOWS_H_INCLUDED
#include "common.h"
#include "t__sec_common.h"
#include "test_timer.h"


#ifndef WIN32
# include <sys/socket.h>
# include <netinet/in.h>
# include <arpa/inet.h>
# include <unistd.h>
#else
# include <windows.h>
# include <winsock.h>
# define in_addr_t in_addr
#endif /*WIN32*/
#include <assert.h>

/** @brief Checks how different Rx security models work */
namespace test_t1_rx {

static struct rx_connection * conn_p = 0;
static struct rx_securityClass * sec_p = 0;
static int op1 = 0;
static int op2 = 0;
static int res = 0;
static int maxerr = 4;

/** @test Add two integers */
void add_test()
{
  int err = 0;
  assert( ((err=t__t1_Add( conn_p,op1,op2,&res))==0) || (--maxerr > 0) );
  assert( (res == (op1+op2)) || (--maxerr > 0) );

  if( res != (op1+op2) || err != 0 )
  {
    printf("t__t1_Add(%d,%d) -> %d (ERROR) [%d]\n",op1,op2,res,err);
    SleepSeconds(0);
  }
}

/** @test Transfer large strings */
void str_test()
{
  char buf[T__T1_BUFFER_SIZE];
  char * res = NULL;
  char * strs = "aB9%";
  char * p = strs;
  int err = 0;

  while( *p )
  {
    memset( buf,*p,T__T1_BUFFER_SIZE );
    res = NULL;
    buf[T__T1_BUFFER_SIZE-1] = 0;
    assert( (err=t__t1_SendString( conn_p,*p,buf ))==0 );
    assert( (err=t__t1_RecvString( conn_p,*p,&res ))==0 );

    assert( strcmp(buf,res) == 0 );

    for( size_t i=0;i<T__T1_BUFFER_SIZE-1;++i )
      if( res[i] != *p ) {
        printf( "Error at: %d read %x indstead of %x\n", i,res[i],*p );
        assert( res[i] == *p );
      }

    assert( res != NULL );
    if( res ) free( res );
    ++p;
  }

}

/** @test Transfer data streams */
void stream_test()
{
  char buff[80000];
  char cmp[80000];
  char * strs = "aB9%";
  char * p = strs;
  int err = 0;
  size_t act_sz = sizeof(buff);

  { /* send stream */
    while( *p && act_sz > 0 )
    {
      memset( buff,*p,act_sz );
      struct rx_call * call_p = rx_NewCall(conn_p);
      assert( call_p != NULL );
      assert( (err = Startt__t1_SendStream( call_p, *p )) == 0 );
      unsigned long ssz = htonl( act_sz );
      assert( (err = rx_Write( call_p, (char *)&ssz, sizeof(ssz) )) == sizeof(ssz) );
      unsigned long p1 = (act_sz*2/3);
      unsigned long p2 = (act_sz - p1);
      assert( (err = rx_Write( call_p, buff, p1)) == (int)p1 );
      assert( (err = rx_Write( call_p, buff+p1, p2)) == (int)p2 );
      assert( (err = Endt__t1_SendStream( call_p )) == 0 );
      assert( (err = rx_EndCall( call_p, err ) ) == 0 );
      act_sz = (act_sz*2/3);
      ++p;
    }
  }

  { /* recv stream */
    p = strs;
    while( *p )
    {
      struct rx_call * call_p = rx_NewCall(conn_p);
      assert( call_p != NULL );
      assert( (err = Startt__t1_RecvStream( call_p, *p )) == 0 );
      unsigned long ssz;
      assert( (err = rx_Read( call_p, (char *)&ssz, sizeof(ssz) )) == sizeof(ssz) );
      ssz = htonl(ssz);
      assert( (err = rx_Read( call_p, buff, sizeof(buff) )) == sizeof(buff) );
      assert( (err = Endt__t1_RecvStream( call_p )) == 0 );
      assert( (err = rx_EndCall( call_p, err ) ) == 0 );
      memset( cmp, *p, sizeof(cmp) );
      assert( memcmp( cmp,buff,sizeof(cmp) ) == 0 );
      ++p;
    }
  }
}

/** @test client main function, that calls the other tests */
int t1_test_client( int argc, char ** argv )
{
#ifdef WIN32
   unsigned long svr_addr = inet_addr("127.0.0.1");
#else /*WIN32*/
   in_addr_t svr_addr = inet_addr("127.0.0.1");
#endif
  unsigned short svr_port = T__T1_SERVER_PORT;

  if ( argc > 1 ) op1 = atoi( argv[1] );
  if ( argc > 2 ) op2 = atoi( argv[2] );

  if ( rx_Init( 0 ) )                              goto error;
  if ( !( sec_p = get_client_security() ) )        goto error;
  if ( !( conn_p = rx_NewConnection(
                     svr_addr,           /* host */
                     htons( svr_port ),  /* port */
                     T__T1_SERVICE_ID,
                     sec_p,              /* secobj */
                     T__T1_NULL_SECOBJ_IDX ) ) )   goto error;

  rt0d4_common_print_results(
    "add_test    ",
    rt0d4_common_test_timer_v0(add_test),
    "" );

  rt0d4_common_print_results(
    "str_test    ",
    rt0d4_common_test_timer_v0(str_test),
    "" );

  rt0d4_common_print_results(
    "stream_test ",
    rt0d4_common_test_timer_v0(stream_test),
    "" );

  rx_Finalize();
  printf("OK\n");
  return 0;

error:
  printf( "ERROR\n" );
  return 1;
}

} // end of test_t1_rx

using namespace test_t1_rx;

int main( int argc, char ** argv )
{
  return t1_test_client(argc, argv);
}

/* EOF */

